/**
 * BinaryOperation.java
 * 
 * Copyright (C) 2012 - Thibault BUREL
 * 
 * This file is part of MathDraft.
 *
 * MathDraft is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MathDraft is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MathDraft.  If not, see <http://www.gnu.org/licenses/>
 *  
 */
package drk.mathdraft.proposition;


public abstract class BinaryOperation extends Proposition
{
	/*************************************************************************
	 *                          M E M B E R S                                *
	 *************************************************************************/
	protected int        priority;
	protected String     symbol;
	protected Proposition left;
	protected Proposition right;
	
	/*************************************************************************
	 *                          M E T H O D S                                *
	 *************************************************************************/
	public BinaryOperation(BinaryOperation e)
	{
		super();
		
		this.symbol   = new String(e.symbol);
		this.priority = e.priority;
		
		setFirst (e.left);
		setSecond(e.right);
	}
	
	public BinaryOperation(String s, int p)
	{
		super();
		
		this.priority = p;
		this.symbol   = new String(s);
	}
	
	public BinaryOperation (String s, int p, Proposition l, Proposition r)
	{
		super();

		this.priority = p;
		this.symbol   = new String(s);
		
		setFirst (l);
		setSecond(r);
	}
	
	public String getIconCommand()
	{
		return this.symbol;
	}	
	
	public Proposition getFirst ()
	{
		return this.left;
	}
	
	public Proposition getFirstChild()
	{
		return this.left;
	}
	
	public Proposition getNextChild(Proposition current)
	{
		if(current == this.left)
		{
			return this.right;
		}
		else
		{
			return null;
		}
	}	
	
	public int getPriority()
	{
		return this.priority;
	}
	
	public Proposition getSecond ()
	{
		return this.right;
	}
	
	public String getShortName()
	{
		return this.symbol;
	}
	
	public String getSymbol ()
	{
		return this.symbol;
	}
	
	public String latexCommands(int rootPriority)
	{
		String result = new String();
		
		if(this.selected == true)
		{
			result += "\\boxed{";
		}
		if(rootPriority > this.priority)
		{
			result += 
				"\\left(" +
				left.latexCommands(this.priority) + 
				this.symbol + 
				right.latexCommands(this.priority)+
				"\\right)";
		}
		else
		{
			result +=
				left.latexCommands(this.priority) + 
				this.symbol + 
				right.latexCommands(this.priority);
		}
		
		if(this.selected == true)
		{
			result +="}";
		}
		
		System.out.println(result);
		return result;
	}
	
	public void setFirst (Proposition e)
	{
		this.left = e.copy();
		this.left.setParent(this);
	}
	
	public void setSecond (Proposition e)
	{
		this.right = e.copy();
		this.right.setParent(this);
	}
	
	public void replaceChild(Proposition o, Proposition n)
	{
		if(this.left == o)
		{
			setFirst(n);
		}
		
		if(this.right == o)
		{
			setSecond(n);
		}
	}
	
	public void unselectAll()
	{
		this.selected = false;
		this.left.unselectAll();
		this.right.unselectAll();
	}
}